//
//  MainGameScene.m
//  Project 5
//
//  Created by Nguyen Tan Loc on 6/21/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "MainGameScene.h"
#import "LevelSelectScene.h"

@implementation MainGameScene
static MainGameScene *m_Instance = nil;

+(CCScene *) scene
{
	CCScene *scene = [CCScene node];
	MainGameScene *layer = [MainGameScene node];
	[scene addChild: layer z:0 tag:0];
	return scene;
}

+(MainGameScene *) getInstance
{
    return m_Instance;
}

-(id) init
{
	if(self = [super init])
    {
        m_Instance = self;
        
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"circle.plist" textureFilename:@"circle.png"];
        
        [[LevelManager shareLevelManager] loadCurrentLevel];
         
        mCircleOrbitManager = [[[LevelManager shareLevelManager] getCircleOrbitManager] retain];
        [mCircleOrbitManager setListOrbitRadius:[[LevelManager shareLevelManager] getListOrbitRadius]];
        [mCircleOrbitManager setPosition:ccp(winSize.width / 2, winSize.height / 2)];
        [self addChild:mCircleOrbitManager];  // retain count increase 1 here

        mBulletManager = [[BulletManager alloc] init];
        [mBulletManager setPosition:ccp(winSize.width / 2, winSize.height / 2)];
        [mBulletManager startUpdate];
        [self addChild:mBulletManager];
        
        b = [[Bullet alloc] initWithType:TYPE_NORMAL color:TYPE_COLOR_BLUE ];
        [b setPosition:ccp(0, 0)];
        [self addChild:b];
        
        self.touchEnabled = true;
        [self scheduleUpdate];
        
        CCMenu *mMenu = [[CCMenu alloc] init];
        [mMenu setPosition:CGPointZero];
        [self addChild:mMenu];
        CCSprite *spriteUpFacebook = [CCSprite spriteWithSpriteFrameName:@"coin@2x.png"];
        CCMenuItem *miTwitter = [CCMenuItemSprite itemWithNormalSprite:spriteUpFacebook selectedSprite:nil target:self selector:@selector(onBack)];
        [miTwitter setPosition:ccp(winSize.width - 40, 50)];
        [mMenu addChild:miTwitter];
	}
    
	return self;
}

-(void) onBack
{
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[LevelSelectScene scene]]];
}

-(void) update:(ccTime)delta
{
    [[PhysicSystem sharedPhysicSystem] update];
}

- (void) ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [[CCDirector sharedDirector] convertTouchToGL:touch];
    
    
    [mBulletManager processTouch:touch.phase position:touchLocation];
}

-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView: [touch view]];
//    CGPoint prevLocation = [touch previousLocationInView: [touch view]];
    
    touchLocation = [[CCDirector sharedDirector] convertTouchToGL:touch];
//    prevLocation = [[CCDirector sharedDirector] convertToGL: prevLocation];
    
//    CGPoint diff = ccpSub(touchLocation, prevLocation);
    
//    [b setPosition:ccp(b.position.x + diff.x, b.position.y + diff.y)];
    [mBulletManager processTouch:touch.phase position:touchLocation];
};

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [[CCDirector sharedDirector] convertTouchToGL:touch];
    [mBulletManager processTouch:touch.phase position:touchLocation];
}

-(CircleOrbitManager *) getCircleOrbitManager
{
    return mCircleOrbitManager;
}


-(void) dealloc
{
    [super dealloc];
    
    [[LevelManager shareLevelManager] removeData];
    
    [mCircleOrbitManager release];
    mCircleOrbitManager = nil;
    
    [[PhysicSystem sharedPhysicSystem] removeAllBody];
    CCLOG(@"MainGameScene dealloc");
}

@end
